package u1;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

/**
 * Writes pages to given output stream. The format isn't the same as the wiki
 * dump (some tags are dropped), but it close enough that it should be readable
 * with a PageReader. This hides the details associated with escaping special
 * characters for storage in XML format (it uses an XMLStreamWriter instead of
 * just writing out the tags itself).
 * 
 * If you get an exception complaining about encoding and you're passing in
 * System.out (standard out), make sure you're calling Java with
 * "-Dfile.encoding=utf8". If you're passing in a file stream, you might have to
 * do something else... not sure what.
 */
public class PageWriter {
  private static final Map<String, String> NO_ATTRIBUTES = new HashMap<String, String>();
  private XMLStreamWriter writer;

  /**
   * @param os not null
   * @param rootName not null
   * @param rootAttributes not null
   * @throws XMLStreamException
   * @throws FactoryConfigurationError
   */
  public PageWriter(OutputStream os, String rootName,
      Map<String, String> rootAttributes) throws XMLStreamException,
      FactoryConfigurationError {

    writer = XMLOutputFactory.newInstance().createXMLStreamWriter(os);
    writer.writeStartDocument("UTF-8", "1.0");
    writer.writeStartElement(rootName);

    for (Map.Entry<String, String> e : rootAttributes.entrySet()) {
      writer.writeAttribute(e.getKey(), e.getValue());
    }
  }

  public void writePage(String title, String text) {
    writePage(title, text, NO_ATTRIBUTES);
  }

  public void writePage(String title, String text,
      Map<String, String> attributes) {
    try {
      writer.writeStartElement("title");
      for (Map.Entry<String, String> e : attributes.entrySet()) {
        writer.writeAttribute(e.getKey(), e.getValue());
      }
      writer.writeCharacters(title);
      writer.writeEndElement();
      writer.writeStartElement("text");
      writer.writeCharacters(text);
      writer.writeEndElement();
    } catch (XMLStreamException e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }

  public void close() throws XMLStreamException {
    writer.writeEndElement();
    writer.close();
  }
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

